package com.cn.redis.service.impl;


import com.cn.redis.properties.CommonProperties;
import com.cn.redis.utils.c;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

@Service("com.cn.redis.gen.a")
public class a {
    private final Map<String, c> G = new HashMap();
    private final ReadWriteLock n = new ReentrantReadWriteLock(false);
    @Resource(
            name = "com.cn.redis.properties.cp"
    )
    CommonProperties commonProperties;

    public a() {
    }

    public long a(Class<?> clazz) {
        return this.e(clazz.getSimpleName());
    }

    public long e(String seqName) {
        c idGenerator = null;
        this.n.readLock().lock();

        try {
            idGenerator = (c)this.G.get(seqName);
        } finally {
            this.n.readLock().unlock();
        }

        if (idGenerator == null) {
            this.n.writeLock().lock();

            try {
                idGenerator = (c)this.G.get(seqName);
                if (idGenerator == null) {
                    idGenerator = new c();
                    idGenerator.init(seqName, this.commonProperties.getStepC(seqName));
                    this.G.put(seqName, idGenerator);
                }
            } catch (Exception var11) {
                var11.printStackTrace();
            } finally {
                this.n.writeLock().unlock();
            }
        }

        return idGenerator.next();
    }

    public List<Object> nextList(String seqName, int listSize) {
        c idGenerator = null;
        this.n.readLock().lock();

        try {
            idGenerator = (c)this.G.get(seqName);
        } finally {
            this.n.readLock().unlock();
        }

        if (idGenerator == null) {
            this.n.writeLock().lock();

            try {
                idGenerator = (c)this.G.get(seqName);
                if (idGenerator == null) {
                    idGenerator = new c();
                    idGenerator.init(seqName, this.commonProperties.getStepC(seqName));
                    this.G.put(seqName, idGenerator);
                }
            } finally {
                this.n.writeLock().unlock();
            }
        }

        return idGenerator.f(listSize);
    }
}
